<?php 
class Admin extends MY_Controller {
	function __construct() {
		parent::__construct();
		$this->load->model('user_model', '', TRUE);
		$this->load->model('type_model', '', TRUE);
		$this->load->library('form_validation');
	}
	
	function login() {
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('name', '用户名', 'required|trim');
        $this->form_validation->set_rules('password', '密码', 'required|trim');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('backend/admin/login');
            return;
        }

        $this->form_validation->set_rules('login_error', '', 'callback_login_check');

        if ($this->form_validation->run() == FALSE) {
        	$this->load->view('backend/admin/login');
        } else {
			redirect();
        }
    }
 
    function login_check() {
        $name = $this->input->post("name");
        $password = $this->input->post("password");
        $condition['name'] = $name;
        $condition['category_id'] = ADMIN_UCID;
        $data = $this->user_model->search_by_condition($condition, TRUE);
        
        if ($data) {
            if ($password != $data['password']) {
                $this->form_validation->set_message('login_check', '用户名或密码错误');
                return FALSE;
            } else {
				$_SESSION['id'] = $data['id'];
				$_SESSION['name'] = $data['name'];
				$_SESSION['ucid'] = ADMIN_UCID;
                return TRUE;
            }
        } else {
            $this->form_validation->set_message('login_check', '该用户不存在');
            return FALSE;
        }
    }
    
	function logout() {
		if ($this->input->is_ajax_request()) {
			$operate = $this->input->post("operate");
			$is_submit = ($this->input->post("is_submit") == 'false') ? FALSE : TRUE;
			
			if (!$is_submit) {
				$this->load->view('backend/admin/logout');
			} else {
				session_destroy();
				$this->output->set_output(json_encode('succeed'));
			}
		}
	}
	
	function modify_pwd() {
		if ($this->input->is_ajax_request()) {
			$operate = $this->input->post("operate");
			$is_submit = ($this->input->post("is_submit") == 'false') ? FALSE : TRUE;
			
			if (!$is_submit) {
				$this->load->view('backend/admin/modify_pwd');
			} else {
				session_destroy();
				$this->output->set_output(json_encode('succeed'));
			}
		}
	}
	
	function generate_admin_core_js() {
		
		$condition['level'] = 0;
		$parent_type = $this->type_model->search_by_condition($condition);
		foreach ($parent_type as $k => $v) {
			$condition['level'] = 1;
			$condition['pid'] = $v['id'];
			$parent_type[$k]['child'] = $this->type_model->search_by_condition($condition);
		}
		
		/*echo "<pre>";
		print_r($parent_type);
		return ;*/
		$data['type'] = $parent_type;
		echo "<pre>";
		
		$data['option'] = array(
			'width' => 120,
			'items' => array(
				array('text' => '新建', 'icon' => ADMIN_CONTEXT_MENU_IMG . 'create.png', 'alias' => 'create', 'action' => 'menuAction'),
				array('text' => '编辑', 'icon' => ADMIN_CONTEXT_MENU_IMG . 'edit.png', 'alias' => 'edit', 'action' => 'menuAction'),
				array('text' => '删除', 'icon' => ADMIN_CONTEXT_MENU_IMG . 'delete.png', 'alias' => 'delete', 'action' => 'menuAction'),
				array('type' => 'splitLine'),
				array('text' => '账户管理', 'icon' => ADMIN_CONTEXT_MENU_IMG . 'account_manage.png', 'alias' => 'account_manage', 'type' => 'group',
					'width' => 120,
					'items' => array(
						array('text' => '修改密码', 'icon' => ADMIN_CONTEXT_MENU_IMG . 'modify_pwd.png', 'alias' => 'modify_pwd', 'action' => 'menuAction'),
						array('text' => '退出登录', 'icon' => ADMIN_CONTEXT_MENU_IMG . 'logout.png', 'alias' => 'logout', 'action' => 'menuAction')
					)
				)
			),
			'onShow' => 'applyrule');
			
		$this->load->view('backend/admin/admin_core.js.php', $data);
		
		$this->load->helper('file');
		$output_stream = $this->output->get_output();
		if (!write_file(FILE_ADMIN_APP_JS . 'admin_core.js', $output_stream)) {
			echo 'Unable to write the file<br />';
		} else {
			echo 'File written!<br />';
		}
	}
	
	function update_editable_obj() {
		///////////////////////company
		$data['id'] = $this->type_model->_get_by('variable', 'company', 'id', TRUE);
		
		$obj_1 = array('selector' => '#front', 'name' => '公司图片', 'additational_button' => FALSE, 'detail' => array('width' => '251', 'height' => '345'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$obj_2 = array('selector' => '.company #intro', 'name' => '公司简介', 'additational_button' => array('save'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$editable_obj = array($obj_1, $obj_2);
		
		$data['editable_obj'] = serialize($editable_obj);
		
		$this->type_model->update($data);
		
		///////////////////////design_team
		$data['id'] = $this->type_model->_get_by('variable', 'design_team', 'id', TRUE);
		
		$obj_1 = array('selector' => '.gallery', 'name' => '设计团队成员', 'additational_button' => FALSE, 'detail' => array('width' => '196', 'height' => '245', 'thumb' => array('width' => '132', 'height' => '164')), 'permission' => array('create' => true, 'edit' => true, 'delete' => false));
		$obj_2 = array('selector' => '#history', 'name' => '团队成员历史', 'additational_button' => array('create'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$obj_3 = array('selector' => '#portrait', 'name' => '设计团队成员详细信息', 'additational_button' => FALSE, 'detail' => array('width' => '196', 'height' => '245', 'thumb' => array('width' => '132', 'height' => '164')), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$obj_4 = array('selector' => '#scrollbar1 .viewport', 'name' => '团队成员设计项目', 'additational_button' => array('create'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$editable_obj = array($obj_1, $obj_2, $obj_3, $obj_4);
		
		$data['editable_obj'] = serialize($editable_obj);
		
		$this->type_model->update($data);
		
		///////////////////////events
		$data['id'] = $this->type_model->_get_by('variable', 'events', 'id', TRUE);
		
		$obj_1 = array('selector' => '#gallery', 'name' => '大事记图片', 'additational_button' => FALSE, 'detail' => array('width' => '106', 'height' => '68'), 'permission' => array('create' => true, 'edit' => true, 'delete' => false));
		$obj_2 = array('selector' => '#scrollbar2 .viewport', 'name' => '大事记文本', 'additational_button' => array('create'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$editable_obj = array($obj_1, $obj_2);
		
		$data['editable_obj'] = serialize($editable_obj);
		
		$this->type_model->update($data);
		
		///////////////////////honor
		$data['id'] = $this->type_model->_get_by('variable', 'honor', 'id', TRUE);
		
		$obj_1 = array('selector' => '#honor_carousel .viewport', 'name' => '荣誉图片', 'additational_button' => FALSE, 'detail' => array('width' => '134', 'height' => '92'), 'permission' => array('create' => true, 'edit' => true, 'delete' => false));
		$obj_2 = array('selector' => '#scrollbar3 .viewport', 'name' => '荣誉文本', 'additational_button' => array('create'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$editable_obj = array($obj_1, $obj_2);
		
		$data['editable_obj'] = serialize($editable_obj);
		
		$this->type_model->update($data);
		
		///////////////////////manager_community
		$data['id'] = $this->type_model->_get_by('variable', 'manager_community', 'id', TRUE);
		
		$obj_1 = array('selector' => '.flip_sub_page', 'name' => '标题及索引图', 'additational_button' => FALSE, 'detail' => array('width' => '122', 'height' => '122'), 'permission' => array('create' => true, 'edit' => true, 'delete' => false));
		$obj_2 = array('selector' => '.flip-block', 'name' => '详细内容', 'additational_button' => array('save'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$editable_obj = array($obj_1, $obj_2);
		
		$data['editable_obj'] = serialize($editable_obj);
		
		$this->type_model->update($data);
		
		///////////////////////project
		$pid = $this->type_model->_get_by('variable', 'project', 'id', TRUE);
		$child = $this->type_model->_get_child_by_pid($pid);
		
		foreach ($child as $v) {
			$data['id'] = $v['id'];
			$obj_1 = array('selector' => '.project .center-content', 'name' => '项目', 'additational_button' => FALSE, 'detail' => array('width' => '230', 'height' => '147'), 'permission' => array('create' => true, 'edit' => true, 'delete' => false));
			$obj_2 = array('selector' => '#project_carousel .viewport li', 'name' => '项目图片', 'additational_button' => FALSE, 'detail' => array('width' => '709', 'height' => '383'), 'permission' => array('create' => true, 'edit' => true, 'delete' => false));
			$editable_obj = array($obj_1, $obj_2);
			
			$data['editable_obj'] = serialize($editable_obj);
			
			$this->type_model->update($data);
		}
		
		///////////////////////service
		$pid = $this->type_model->_get_by('variable', 'service', 'id', TRUE);
		$child = $this->type_model->_get_child_by_pid($pid);
		
		foreach ($child as $v) {
			$data['id'] = $v['id'];
			$obj_1 = array('selector' => '#intro #content', 'name' => '服务说明', 'additational_button' => array('save'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
			$obj_2 = array('selector' => '#service_carousel .viewport', 'name' => '服务图片', 'additational_button' => FALSE, 'detail' => array('width' => '141', 'height' => '94'), 'permission' => array('create' => true, 'edit' => true, 'delete' => false));
			$editable_obj = array($obj_1, $obj_2);
			
			$data['editable_obj'] = serialize($editable_obj);
			
			$this->type_model->update($data);
		}
		
		///////////////////////media
		$pid = $this->type_model->_get_by('variable', 'media', 'id', TRUE);
		$child = $this->type_model->_get_child_by_pid($pid);
		
		foreach ($child as $v) {
			$data['id'] = $v['id'];
			$obj_1 = array('selector' => '.center-content', 'name' => '新闻报道', 'additational_button' => array('create'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
			$obj_2 = array('selector' => '#media_carousel .viewport .news > img', 'name' => '新闻报道索引图片', 'additational_button' => FALSE, 'detail' => array('width' => '91', 'height' => '174'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
			$obj_3 = array('selector' => '#media_carousel .viewport .news > div', 'name' => '新闻报道文本', 'additational_button' => array('save'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
			$obj_4 = array('selector' => '#media_modal #pic', 'name' => '新闻报道图片', 'additational_button' => FALSE, 'detail' => array('width' => '204', 'height' => '212'), 'permission' => array('create' => true, 'edit' => true, 'delete' => false));
			$editable_obj = array($obj_1, $obj_2, $obj_3, $obj_4);
			
			$data['editable_obj'] = serialize($editable_obj);
			
			$this->type_model->update($data);
		}
		
		///////////////////////course
		$data['id'] = $this->type_model->_get_by('variable', 'course', 'id', TRUE);
		
		$obj_1 = array('selector' => '.inner-content', 'name' => '课程', 'additational_button' => array('create'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$obj_2 = array('selector' => '.inner-content .courses', 'name' => '课程文本', 'additational_button' => array('save'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$obj_3 = array('selector' => '#course_modal #gallery', 'name' => '课程图片', 'additational_button' => FALSE, 'detail' => array('width' => '342', 'height' => '347'), 'permission' => array('create' => true, 'edit' => true, 'delete' => false));
		$editable_obj = array($obj_1, $obj_2, $obj_3);
		
		$data['editable_obj'] = serialize($editable_obj);
		
		$this->type_model->update($data);
		
		///////////////////////exchange
		$data['id'] = $this->type_model->_get_by('variable', 'exchange', 'id', TRUE);
		
		$obj_1 = array('selector' => '.inner-content', 'name' => '交流', 'additational_button' => array('create'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$obj_2 = array('selector' => '.inner-content .exchanges', 'name' => '交流文本', 'additational_button' => array('save'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		//$obj_3 = array('selector' => '#course_modal #gallery', 'name' => '交流图片', 'additational_button' => FALSE, 'detail' => array('width' => '342', 'height' => '347'), 'permission' => array('create' => true, 'edit' => true, 'delete' => false));
		$editable_obj = array($obj_1, $obj_2);
		
		$data['editable_obj'] = serialize($editable_obj);
		
		$this->type_model->update($data);
		
		///////////////////////corporate_culture
		$data['id'] = $this->type_model->_get_by('variable', 'corporate_culture', 'id', TRUE);
		
		$obj_1 = array('selector' => '.inner-content img', 'name' => '企业文化图片', 'additational_button' => FALSE, 'detail' => array('width' => '706', 'height' => '346'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$editable_obj = array($obj_1);
		
		$data['editable_obj'] = serialize($editable_obj);
		
		$this->type_model->update($data);
		
		///////////////////////staff_activities
		$data['id'] = $this->type_model->_get_by('variable', 'staff_activities', 'id', TRUE);
		
		$obj_1 = array('selector' => '.staff_activities .center-content', 'name' => '员工活动', 'additational_button' => FALSE, 'detail' => array('width' => '282', 'height' => '172'), 'permission' => array('create' => true, 'edit' => true, 'delete' => false));
		$obj_2 = array('selector' => '..inner-content .activities', 'name' => '员工活动详细信息', 'additational_button' => FALSE, 'detail' => array('width' => '616', 'height' => '408'), 'permission' => array('create' => true, 'edit' => true, 'delete' => false));
		$editable_obj = array($obj_1, $obj_2);
		
		$data['editable_obj'] = serialize($editable_obj);
		
		$this->type_model->update($data);
		
		///////////////////////contact
		$data['id'] = $this->type_model->_get_by('variable', 'contact', 'id', TRUE);
		
		$obj_1 = array('selector' => '.inner-content img', 'name' => '联系方式图片', 'additational_button' => FALSE, 'detail' => array('width' => '426', 'height' => '272'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$editable_obj = array($obj_1);
		
		$data['editable_obj'] = serialize($editable_obj);
		
		$this->type_model->update($data);
		
		///////////////////////company_location
		$data['id'] = $this->type_model->_get_by('variable', 'company_location', 'id', TRUE);
		
		$obj_1 = array('selector' => '.inner-content img', 'name' => '公司位置图片', 'additational_button' => FALSE, 'detail' => array('width' => '466', 'height' => '391'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$editable_obj = array($obj_1);
		
		$data['editable_obj'] = serialize($editable_obj);
		
		$this->type_model->update($data);
		
		///////////////////////careers
		$data['id'] = $this->type_model->_get_by('variable', 'careers', 'id', TRUE);
		
		$obj_1 = array('selector' => '.inner-content img', 'name' => '招贤纳士图片', 'additational_button' => FALSE, 'detail' => array('width' => '701', 'height' => '391'), 'permission' => array('create' => false, 'edit' => true, 'delete' => false));
		$editable_obj = array($obj_1);
		
		$data['editable_obj'] = serialize($editable_obj);
		
		$this->type_model->update($data);
	}
}